export let recommend = {
  namespaced: true,
  state: {
    recommend_data: {},
    isRecData: true,
    available_data: [],
    available_flag: false
  },
  actions: {
    getRecommendDate(store, data = {}) {
      let url = "/recommend/songs";
      return new Promise((resolve, reject) => {
        axios.get(url).then(({
          data
        }) => {
          if (data.recommend.length == 0) {
            store.state.isRecData = false;
          } else {
            store.state.isRecData = true;
          }

          let list = [];
          let promiseAll = data.recommend.map((item, index) => {
            return axios.get("/check/music?id=" + item.id).then(({
              data
            }) => {
              item.available = true;
              list.push(item.id);
            }).catch(error => {
              item.available = false;
            });
          });
          axios.all(promiseAll).then(resArr => {
            store.state.available_flag = true;
            store.state.available_data = list;
          }).catch(error => {});

          store.commit('setRecommendDate', data);
          resolve(data);
        }).catch((error) => {
          if (error.response) {
            reject(error.response.data);
          }
        });
      });
    }
  },
  mutations: {
    setRecommendDate(state, data = {}) {
      state.recommend_data = data;
    }
  }
}
